home *** CD-ROM | disk | FTP | other *** search
- /* TestBed.c
- * Word Services Testbed - main module for Writeswell Jr.
- *
- * ©1992 Working Software, Inc.
- * This source code is copyrighted. Permission is granted to use the Word Services
- * portion of the Writeswell Jr. source code in your own programs, but you
- * may not distribute the Writeswell Jr. word-processor code as a
- * commercial product. If you modify the code, please do not call it
- * Writeswell Jr. (or Writeswell.) This will ensure that people understand the
- * program and don’t have to deal with a number of different versions with
- * who-knows-what going on in the code.
- *
- * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
- */
-
- #include <EPPC.h>
- #include <AppleEvents.h>
- #include <AEObjects.h>
- #include <Printing.h>
- #include "AERegistry.h"
- #include "WordServices.h"
- #include "TestBed.h"
- #include "TBConstants.h"
- #include "MyFiles.h"
- #include "GenHandlers.h"
- #include "AppEvents.h"
- #include "AEObj.h"
- #include "InitMenu.h"
- #include "Gripe.h"
- #include "Scroll.h"
- #include "Prefs.h"
- #include "DoChecking.h"
- #include "ServiceMgr.h"
- #include "ObWind.h"
- #include "ObText.h"
- #include "ObNull.h"
- #include "FontMenu.h"
- #include "Options.h"
- #include "ServiceDialog.h"
- #include "PrintWWJr.h"
- #include "UnloadStuff.h"
- #include "InitWWJr.h"
- #include "MyGestalt.h"
- #include "PageSetup.h"
- #include "TTPictures.h"
- #include "WWJrAbout.h"
- #include "MyTextEdit.h"
- #include "RegMenu.h"
-
- #define GLOBALS_HERE
- #include "TBGlobals.h"
- #undef GLOBALS_HERE
-
- Boolean DoMouseDown( EventRecord *eventPtr );
- Boolean DoMenuCommand( long menuSpot );
- Boolean DoAppleMenu( short theItem );
- Boolean DoFileMenu( short theItem );
- Boolean DoEditMenu( short theItem );
- Boolean DoServMenu( short theItem );
- Boolean DoKeyDown( long message, short modifiers );
- Boolean DoActivateEvt( short modifiers );
- Boolean DoUpdateEvt( long message );
- Boolean DoOSEvt( EventRecord *eventPtr );
- void GrowDocWindow( WindowPtr theWindow, Point where );
- void SetTESize( WindowPtr theWindow );
- void MyDrawGrowIcon( WindowPtr theWindow );
- void DoQuit( void );
- void DoZoom( WindowPtr theWindow, short deskPart );
- void ResetWindowParts( WindowPtr theWindow );
-
- void main( void )
- {
- OSErr err;
-
- MaxApplZone();
- //InitGraf(&thePort);
- InitGraf(&qd.thePort);
- InitFonts();
- /* FlushEvents(everyEvent, 0); */
- InitWindows();
- TEInit();
- InitDialogs(0L);
-
- gDocDirty = false;
-
- gAppFileRefNum = CurResFile();
-
- gQuitRequested = false;
- gPrintRequested = false;
-
- if ( !OpenPrefFile() ){
- Gripe( "\pCannot open Writeswell, Jr. Preferences" );
- ExitToShell();
- }
-
- InitCursor();
-
- if ( System7Installed() ){
- err = InitForSystem7();
- }else{
- #ifndef GENERATINGCFM
- err = InitForSystem6();
- #endif
- }
-
- if ( err )
- ExitToShell();
-
- while ( EventLoop() )
- ;
-
- TearDown(); // Destroy routine descriptors, etc.
-
- ExitToShell();
- }
-
- Boolean EventLoop( void )
- {
- EventRecord theEvent;
-
- UnloadStuff();
-
- if ( gPrintRequested ){
- gPrintRequested = false;
- DoPrint();
- }
-
- if ( gQuitRequested ){
- DoQuit();
- return false; /* We should never reach this statement */
- }
-
- WaitNextEvent( everyEvent, &theEvent, 30, (RgnHandle)NULL );
-
-
- if ( gDocWindow )
- TEIdle( (TEHandle)GetWRefCon( gDocWindow ) );
-
- switch( theEvent.what ){
- case nullEvent:
- DoNullEvent( &theEvent );
- break;
- case mouseDown:
- return DoMouseDown( &theEvent );
- break;
- case mouseUp:
- DoMouseUp();
- break;
- case autoKey:
- case keyDown:
- return DoKeyDown( theEvent.message, theEvent.modifiers );
- break;
- case updateEvt:
- DoUpdateEvt( theEvent.message );
- break;
- case diskEvt:
- DoDiskEvt();
- break;
- case activateEvt:
- DoActivateEvt( theEvent.modifiers );
- break;
- case osEvt:
- DoOSEvt( &theEvent );
- break;
- case kHighLevelEvent:
- DoHighLevelEvent( &theEvent );
- break;
- default:
- /*Gripe("\pGot unknown event type" );*/
- break;
- }
-
- return true;
- }
-
- Boolean DoNullEvent( EventRecord *theEventPtr )
- {
- /* One could execute some sort of background task here */
-
- return true;
- }
-
- Boolean DoMouseDown( EventRecord *eventPtr )
- {
- WindowPtr theWindow;
- Boolean result = true;
- Rect dragRect;
- TEHandle textH;
- short partCode;
- ControlHandle ctlHdl;
- short curFile;
- short deskPart;
- Boolean shifted;
-
- deskPart = FindWindow( eventPtr->where, &theWindow );
- switch( deskPart ){
- case inDesk:
- break;
- case inMenuBar:
- FixMenuMarks();
- curFile = CurResFile();
- UseResFile( gPrefFileRefNum ); /* Make SICN resource accessible */
- result = DoMenuCommand( MenuSelect( eventPtr->where ) );
- UseResFile( curFile );
- FixMenuMarks();
- break;
- case inSysWindow:
- SystemClick( eventPtr, theWindow );
- break;
- case inContent:
- if ( theWindow != FrontWindow() ){
- SelectWindow( theWindow );
- } else {
- GlobalToLocal( &(eventPtr->where) );
- partCode = FindControl( eventPtr->where, theWindow, &ctlHdl );
- if ( partCode == 0 ){
- textH = (TEHandle)GetWRefCon( theWindow );
-
- gScrollWindow = theWindow; /* For use by TrackContentClick */
-
- /* 1.1.1 MDC We use an intermediate variable rather than just passing
- * eventPtr->modifiers & shiftKey because this is a two-byte
- * value, with a bit set in the high byte. Since a boolean
- * is a single byte, this resulted in always passing 0, so we
- * didn't see the shift key.
- */
-
- if ( eventPtr->modifiers & shiftKey ){
- shifted = true;
- }else{
- shifted = false;
- }
-
- TEClick( eventPtr->where,
- shifted,
- textH );
- }else{
- DoControl( theWindow, ctlHdl, partCode, eventPtr->where );
- }
- }
- break;
- case inDrag:
- DragWindow( theWindow, eventPtr->where, &qd.screenBits.bounds ); /* NOT correct but works */
- break;
- case inGrow:
- GrowDocWindow( theWindow, eventPtr->where );
- break;
- case inGoAway:
- if ( TrackGoAway( theWindow, eventPtr->where ))
- DoCloseWindow();
- break;
- case inZoomIn:
- case inZoomOut:
- if ( TrackBox( theWindow, eventPtr->where, deskPart ) )
- DoZoom( theWindow, deskPart );
- break;
- }
- return result;
- }
-
- Boolean DoMenuCommand( long menuSpot )
- {
- short theMenu;
- short theItem;
- Boolean result;
-
- theMenu = HiWord( menuSpot );
-
- theItem = LoWord( menuSpot );
-
- switch ( theMenu ){
- case 0:
- result = true;
- break;
- case kAppleMenuID:
- result = DoAppleMenu( theItem );
- break;
- case kFileMenuID:
- result = DoFileMenu( theItem );
- break;
- case kEditMenuID:
- result = DoEditMenu( theItem );
- break;
- case kFontMenuID:
- result = DoFontMenu( theItem );
- break;
- case kStyleMenuID:
- result = DoStyleMenu( theItem );
- break;
- case kServMenuID:
- result = DoServMenu( theItem );
- break;
- case kRegMenuID:
- result = DoRegMenu( theItem );
- break;
- }
-
- HiliteMenu( 0 );
- return result;
- }
-
- Boolean DoAppleMenu( short theItem )
- {
- Handle appleHandle;
- Str255 itemName;
-
- switch ( theItem ){
- case kAMAboutMe:
- DoAboutMe();
- break;
- case kAMDash:
- break;
- default:
- appleHandle = GetResource( 'MENU', kAppleMenuID );
- if ( !appleHandle ){
- Gripe( "\pCannot get handle to apple menu" );
- return false;
- }
-
- GetItem( (MenuHandle) appleHandle, theItem, itemName );
- OpenDeskAcc( itemName );
- if ( gDocWindow ){
- SetPort( gDocWindow );
- }
- break;
- }
- return true;
- }
-
- Boolean DoFileMenu( short theItem )
- {
- switch ( theItem ){
- case kFMNew:
- if ( gDocWindow )
- return true;
-
- return MakeNewWindow() == noErr;
- break;
- case kFMOpen:
- DoOpenDialog();
- break;
- case kFMClose:
- DoCloseWindow();
- break;
- case kFMSave:
- DoSave();
- break;
- case kFMSaveAs:
- DoSaveAs();
- break;
- case kFMPageSetup:
- DoPageSetup();
- break;
- case kFMPrint:
- DoPrint();
- break;
- case kFMQuit:
- DoQuit();
- break;
- }
-
- return true;
- }
-
- Boolean DoEditMenu( short theItem )
- {
- TEHandle textH;
-
- /* 2.0 MDC added check for read-only documents */
-
- if ( !SystemEdit( theItem - 1 ) ){
- if ( gDocWindow ){
- textH = (TEHandle)GetWRefCon( gDocWindow );
-
- switch( theItem ){
- case kEMCut:
- if ( !gReadOnly ){
- gDocDirty = true;
- TECut( textH );
- }else{
- TECopy( textH );
- }
- break;
- case kEMCopy:
- TECopy( textH );
- break;
- case kEMPaste:
- if ( !gReadOnly ){
- gDocDirty = true;
- TEStylPaste( textH );
- }
- break;
- case kEMClear:
- if ( !gReadOnly ){
- gDocDirty = true;
- TEDelete( textH );
- }
- break;
- case kEMSelectAll:
- TESetSelect( 0, 32767, textH );
- break;
- case kEMOptions:
- OptionsDialog();
- break;
- }
-
- SetVertScroll( gDocWindow, gVertScroll );
- }else{
- /* Handle the operations that we can do while the window is closed */
- switch( theItem ){
- case kEMOptions:
- OptionsDialog();
- break;
- }
-
- }
- }
- return true;
- }
-
- Boolean DoServMenu( short theItem )
- {
- OSErr err;
-
- switch( theItem ){
- #ifdef NEVER /* This will return in a future version */
- case kSMCheckSel:
- ToggleSelectCheck();
- break;
- #endif
- case kSMNewBatch:
- err = GetNewBatchService();
- if ( err )
- Gripe( "\pGetNewBatchService failed" );
- break;
- #ifdef NEVER /* This will return in a future version */
- case kSMNewInteractive:
- if ( !gDocWindow )
- return true;
- break;
- #endif
- case kSMRemoveService:
- RemoveServices();
- break;
- #ifdef NEVER /* This will return in a future version */
- case kSMCheckWord:
- if ( !gDocWindow )
- return true;
- break;
- #endif
- case kSMDash:
- break;
- default:
- /* Here is where we actually do some spellchecking! */
- if ( !gDocWindow )
- return true;
-
- DoSpellCheck( theItem - kSMDash );
-
- break;
- }
- return true;
- }
-
- Boolean DoMouseUp( void )
- {
- return true;
- }
-
- Boolean DoKeyDown( long message, short modifiers )
- {
- char theChar;
- Boolean result;
-
- theChar = message & charCodeMask;
-
- if ( modifiers & cmdKey ){
- FixMenuMarks(); /* We need to have the dis/enabling up to date */
- result = DoMenuCommand( MenuKey( theChar ) );
- }else {
-
- /* 2.0 MDC don't allow typing in a readonly document */
-
- if ( gDocWindow && ( gDocWindow == FrontWindow() )){
- if ( !gReadOnly )
- gDocDirty = true;
- MyTEKey( gDocWindow, gVertScroll, theChar, gReadOnly );
- }
- result = true;
- }
-
- return result;
- }
-
- Boolean DoUpdateEvt( long message )
- {
- GrafPtr curPort;
-
- BeginUpdate( (WindowPtr)message );
-
- if ( (WindowPtr)message == gDocWindow ){
- GetPort( &curPort );
- SetPort( gDocWindow );
- EraseRect( &qd.thePort->portRect );
- TEUpdate( &qd.thePort->portRect, (TEHandle)GetWRefCon( (WindowPtr)message ) );
-
- ShowPictures( gDocWindow, gNumPictures, gResRefNum );
-
- MyDrawGrowIcon( (WindowPtr)message );
- DrawControls( gDocWindow );
- SetPort( curPort );
- }
-
- EndUpdate( (WindowPtr)message );
- return true;
- }
-
- void MyDrawGrowIcon( WindowPtr theWindow )
- {
- RgnHandle clipRgn;
- Rect newClip;
-
- /* This will fudge the DrawGrowIcon routine so we only draw the outline for the
- * vertical scroll bar. It's easier than writing a custom WDEF.
- * We have to set a new clip region because the update region will mask off some
- * of what we might want to draw.
- *
- * thePort must be the window that is getting the icon drawn in it.
- */
-
- clipRgn = NewRgn();
- if ( !clipRgn )
- return;
-
- GetClip( clipRgn );
-
- newClip.top = theWindow->portRect.top;
- newClip.right = theWindow->portRect.right;
- newClip.bottom = theWindow->portRect.bottom;
- newClip.left = newClip.right - 15;
-
- ClipRect( &newClip );
-
- DrawGrowIcon( theWindow );
-
- SetClip( clipRgn );
-
- DisposeRgn( clipRgn ); /* 1.1.1 MDC fix a leak */
-
- return;
- }
-
- Boolean DoDiskEvt( void )
- {
- return true;
- }
-
- Boolean DoActivateEvt( short modifiers )
- {
-
- if ( gDocWindow ){
-
- if ( modifiers & activeFlag ){
- TEActivate( (TEHandle)GetWRefCon( gDocWindow ) );
- } else {
- TEDeactivate( (TEHandle)GetWRefCon( gDocWindow ) );
- }
- }
- return true;
- }
-
- void GrowDocWindow( WindowPtr theWindow, Point where )
- {
- long newSize;
- short newWidth;
- short newHeight;
- GrafPtr curPort;
- Rect sizeRect;
-
- SetRect( &sizeRect, 80, 80, 32767, 32767 );
-
- newSize = GrowWindow( theWindow, where, &sizeRect );
- if ( newSize == 0L )
- return;
-
- newWidth = (short) newSize;
- newHeight = (short) ( newSize >> 16 );
-
- GetPort( &curPort );
- SetPort( theWindow );
-
- SizeWindow( theWindow, newWidth, newHeight, true );
-
- ResetWindowParts( theWindow );
-
- SetPort( curPort );
-
- return;
- }
-
- void ResetWindowParts( WindowPtr theWindow )
- {
- GrafPtr curPort;
-
- /* Resize all of the components of the document window after the window itself
- * has been resized.
- */
-
- GetPort( &curPort );
- SetPort( theWindow );
-
- SetTESize( theWindow );
-
- /* We have to redraw the whole window, since the grow icon, scroll bars, and
- * text rectange have all changed
- */
-
- InvalRect( &( theWindow->portRect ) );
-
- SizeVertScroll();
- SetVertScroll( theWindow, gVertScroll );
-
- SetPort( curPort );
-
- return;
- }
-
- Boolean DoOSEvt( EventRecord *eventPtr )
- {
- Boolean inBackground;
-
- switch ((eventPtr->message >> 24) & 0x0FF) { /* high byte of message */
- case suspendResumeMessage: /* suspend/resume is also an activate/deactivate */
- if ( gDocWindow ){
-
- inBackground = (eventPtr->message & resumeFlag) == 0;
- if (inBackground) {
- #ifdef NEVER /* Don't use for styled textedit */
- ZeroScrap();
- TEToScrap();
- #endif
- if ( gDocWindow ){
- TEDeactivate( (TEHandle)GetWRefCon( gDocWindow ) );
- }
- } else {
- #ifdef NEVER /* Don't use for styled textedit */
- TEFromScrap();
- #endif
- if ( gDocWindow ){
- TEActivate( (TEHandle)GetWRefCon( gDocWindow ) );
- }
- }
- }
-
- default:
- break;
- }
- return true;
- }
-
- Boolean DoHighLevelEvent( EventRecord *theEventPtr )
- {
- OSErr err;
-
- err = AEProcessAppleEvent( theEventPtr );
-
- if ( err ){
- return true; /* An error result is OK... */
- }
-
- return true;
- }
-
- void SetTESize( WindowPtr theWindow )
- {
- Rect txRect;
- TEHandle hTE;
- short rectOffset;
-
- GetTERect( &( theWindow->portRect ), &txRect );
-
- hTE = (TEHandle)GetWRefCon( theWindow );
-
- rectOffset = (*hTE)->destRect.top - (*hTE)->viewRect.top;
-
- (*hTE)->viewRect = txRect;
-
- txRect.top += rectOffset;
- txRect.bottom += rectOffset;
-
- (*hTE)->destRect = txRect;
-
- TECalText( hTE );
-
- return;
- }
-
- void GetTERect( Rect *portRectPtr, Rect *txRectPtr )
- {
- /* Return the rect for the textedit field that will fit in the
- * given portRect, allowing for the scroll bar, and a little bit
- * of margin around all the edges.
- */
-
- *txRectPtr = *portRectPtr;
-
- InsetRect( txRectPtr, kTextInset, kTextInset );
-
- txRectPtr->right -= 16;
-
- return;
- }
-
- void DoQuit( void )
- {
- Boolean reallyQuit;
-
- if ( gDocWindow )
- reallyQuit = DoCloseWindow();
-
- if ( reallyQuit )
- ExitToShell();
-
- return;
- }
-
- void DoZoom( WindowPtr theWindow, short deskPart )
- {
- Boolean isFront;
-
- if ( FrontWindow() == theWindow )
- isFront = true;
- else
- isFront = false;
-
- ZoomWindow( theWindow, deskPart, isFront );
-
- ResetWindowParts( theWindow );
-
- return;
- }
-
-